home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / BitsGetter.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  3.6 KB  |  164 lines

  1. /* 
  2.  *  BitsGetter.cpp
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24.  
  25. #include "BitsGetter.h"
  26.  
  27. //////////////////////////////////////////////////////////////////////
  28. // Construction/Destruction
  29. //////////////////////////////////////////////////////////////////////
  30.  
  31. CBits::CBits(CBitSource *source, int streamID, int subStreamID)
  32. {
  33.     src=source;
  34.     ld=new bits_data;
  35.     CBits::streamID    = streamID;
  36.     CBits::subStreamID = subStreamID;
  37.     
  38. }
  39.  
  40. CBits::~CBits()
  41. {
  42.     delete ld;
  43. }
  44.  
  45. void CBits::Initialize_Buffer(){
  46.  
  47.   audioBufferSize=1024;
  48.   ld->Incnt = 0;
  49.   ld->Rdptr = ld->Rdbfr + audioBufferSize;
  50.   ld->Rdmax = ld->Rdptr;
  51.   ld->Bfr = 0;
  52.   eos=false;
  53.   Flush_Buffer(0); /* fills valid data into bfr */
  54. };
  55.  
  56.  
  57. void CBits::Fill_Buffer(){
  58.  
  59.   int Buffer_Level;
  60.   do{
  61.       Buffer_Level =src->ReadPES((unsigned char **)&ld->Rdbfr, &PES);
  62.   }while(((PES.streamID!=streamID) || (PES.subStreamID!=subStreamID)) && Buffer_Level);
  63.  
  64.   audioBufferSize=PES.payloadSize;
  65.   ld->Rdmax=ld->Rdbfr + audioBufferSize;
  66.  
  67.   ld->Rdptr = ld->Rdbfr;
  68.  
  69.   
  70.   /* end of the bitstream file */
  71.   if (Buffer_Level < audioBufferSize || Buffer_Level==0)
  72.   {
  73.     /* just to be safe */
  74.     if (Buffer_Level < 0)
  75.       Buffer_Level = 0;
  76.  
  77.     /* pad until the next to the next 32-bit word boundary */
  78.     while (Buffer_Level & 3)
  79.       ld->Rdbfr[Buffer_Level++] = 0;
  80.  
  81.  
  82.     /* pad the buffer */
  83.     while (Buffer_Level < audioBufferSize)
  84.     {
  85.       ld->Rdbfr[Buffer_Level++] = 0>>24;
  86.       ld->Rdbfr[Buffer_Level++] = 0>>16;
  87.       ld->Rdbfr[Buffer_Level++] = 0>>8;
  88.       ld->Rdbfr[Buffer_Level++] = 0&0xff;
  89.     }
  90.     eos=true;
  91.   }
  92. }
  93.  
  94.  
  95. unsigned int CBits::Get_Bits(int N)
  96. {
  97.   unsigned int Val;
  98.  
  99.   Val = Show_Bits(N);
  100.   Flush_Buffer(N);
  101.  
  102.   return Val;
  103. }
  104. unsigned int CBits::Get_Bits1(){
  105.   return Get_Bits(1);
  106. }
  107. unsigned int CBits::Show_Bits(int N){
  108.   return ld->Bfr >> (32-N);
  109. }
  110. int CBits::Get_Word(){
  111.   int Val;
  112.  
  113.   Val = Get_Byte();
  114.   return (Val<<8) | Get_Byte();
  115. }
  116.  
  117. int CBits::Get_Byte(){
  118.   while(ld->Rdptr >= ld->Rdbfr+audioBufferSize)
  119.   {
  120.     //src->ReadPES((char *)ld->Rdbfr,audioBufferSize);
  121.     ld->Rdptr -= audioBufferSize;
  122.     ld->Rdmax -= audioBufferSize;
  123.   }
  124.   return *ld->Rdptr++;
  125. }
  126.  
  127.  
  128. void CBits::Flush_Buffer(int N){
  129.   int Incnt;
  130.  
  131.   ld->Bfr <<= N;
  132.  
  133.   Incnt = ld->Incnt -= N;
  134.  
  135.   if (Incnt <= 24)
  136.   {
  137.     if (ld->Rdptr < ld->Rdbfr+audioBufferSize-4)
  138.     {
  139.       do
  140.       {
  141.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  142.         Incnt += 8;
  143.       }
  144.       while (Incnt <= 24);
  145.     }
  146.     else
  147.     {
  148.       do
  149.       {
  150.         if (ld->Rdptr >= ld->Rdbfr+audioBufferSize)
  151.           Fill_Buffer();
  152.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  153.         Incnt += 8;
  154.       }
  155.       while (Incnt <= 24);
  156.     }
  157.     ld->Incnt = Incnt;
  158.   }
  159.  
  160. #ifdef VERIFY 
  161.   ld->Bitcnt += N;
  162. #endif /* VERIFY */
  163. }
  164.